<?php
/**
 * NotificationController.php
 *
 * @copyright   2023 thingsworld.com.cn - All Rights Reserved
 * @link        http://thingsworld.com.cn/
 * @author      stiffer.chen <chenl@thingsworld.com.cn>
 * @created     2023-02-20 14:14:50
 * @modified    2023-02-20 14:14:50
 *
 */

namespace App\Http\Controllers;

use App\Repositories\NotificationRepo;
use Illuminate\Http\Request;

class NotificationController extends BaseModuleController
{
    /**
     * @OA\Get(
     *     path="/notifications",
     *     tags={"Notification"},
     *     summary="通知列表",
     *     description="通知列表",
     *     operationId="notification_index",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="type", in="query", description="类型筛选(work_order、event, task, inventory, suggest, question_feedback, polling)", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="page", in="query", description="当前页数", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="limit", in="query", description="每页数量(默认10)", required=false, @OA\Schema(default=10, type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function index()
    {
        try {
            $notifications = NotificationRepo::getList();
            return list_message('获取成功', true, $notifications);
        } catch (\Exception $e) {
            return list_message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Post(
     *     path="/notifications/read_all",
     *     tags={"Notification"},
     *     summary="标记所有通知为已读",
     *     description="标记所有通知为已读",
     *     operationId="notification_read_all",
     *     security={{"bearerAuth":{}}},
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function read_all(Request $request)
    {
        try {
            NotificationRepo::readAll();
            return message('标记成功', true);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Post(
     *     path="/notifications/store_test",
     *     tags={"Notification"},
     *     summary="通知测试",
     *     description="通知测试",
     *     operationId="notification_store",
     *     security={{"bearerAuth":{}}},
     *     @OA\RequestBody(
     *          required=true,
     *          @OA\JsonContent(
     *              type="object",
     *              required={"user_id", "business_id", "business_type", "title", "content"},
     *              @OA\Property(property="user_id", type="integer", example=0, description=""),
     *              @OA\Property(property="business_id", type="string", example="", description=""),
     *              @OA\Property(property="business_type", type="string", example="", description=""),
     *              @OA\Property(property="title", type="string", example="", description=""),
     *              @OA\Property(property="content", type="string", example="", description="")
     *          )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function store_test()
    {
        $notificationData = request()->getContent();
        $notificationArr = json_decode($notificationData, true);

        $userId = $notificationArr['user_id'] ?? 0;
        $businessId = $notificationArr['business_id'] ?? 0;
        $businessType = $notificationArr['business_type'] ?? '';
        $title = $notificationArr['title'] ?? '';
        $content = $notificationArr['content'] ?? '';
        NotificationRepo::store($userId, $businessId, $businessType, $title, $content);
    }

    /**
     * @OA\Post(
     *     path="/notifications/{id}/mark_read",
     *     tags={"Notification"},
     *     summary="标记通知为已读",
     *     description="标记通知为已读",
     *     operationId="notification_mark_read",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id", in="path", description="通知ID", required=true, @OA\Schema(default="", type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function mark_read(Request $request, $id)
    {
        try {
            $notification = NotificationRepo::markRead($id);
            return message('更新成功', true, $notification);
        } catch (\Exception $e) {
            return message($e->getMessage(), false, null);
        }
    }

    /**
     * @OA\Get(
     *     path="/notifications/status_count",
     *     tags={"Notification"},
     *     summary="获取通知状态数量",
     *     description="获取通知状态数量",
     *     operationId="notification_status_count",
     *     security={{"bearerAuth":{}}},
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function status_count(Request $request)
    {
        return message('获取成功', true, NotificationRepo::statusCount());
    }
}